package com.xd.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by DGF on 2015/4/17.
 */
public class MainActivity extends Activity implements View.OnClickListener {

    private EditText txtResult = null;
    private Button btnClear = null;
    private Button btnDelete = null;
    private Button btnDivide = null;
    private Button btnMultiply = null;
    private Button btnPlus = null;
    private Button btnSubtract = null;
    private Button btnPoint = null;
    private Button btn0 = null;
    private Button btn1 = null;
    private Button btn2 = null;
    private Button btn3 = null;
    private Button btn4 = null;
    private Button btn5 = null;
    private Button btn6 = null;
    private Button btn7 = null;
    private Button btn8 = null;
    private Button btn9 = null;
    private Button btnEqual = null;

    /**
     * 清空标志
     */
    private Boolean flagClear = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        txtResult = (EditText)this.findViewById(R.id.txt_result);
        btnClear = (Button)this.findViewById(R.id.btn_clear);
        btnDelete = (Button)this.findViewById(R.id.btn_del);
        btnDivide = (Button)this.findViewById(R.id.btn_divide);
        btnMultiply = (Button)this.findViewById(R.id.btn_multiply);
        btnPlus = (Button)this.findViewById(R.id.btn_plus);
        btnSubtract = (Button)this.findViewById(R.id.btn_substract);
        btnPoint = (Button)this.findViewById(R.id.btn_point);
        btn0 = (Button)this.findViewById(R.id.btn_0);
        btn1 = (Button)this.findViewById(R.id.btn_1);
        btn2 = (Button)this.findViewById(R.id.btn_2);
        btn3 = (Button)this.findViewById(R.id.btn_3);
        btn4 = (Button)this.findViewById(R.id.btn_4);
        btn5 = (Button)this.findViewById(R.id.btn_5);
        btn6 = (Button)this.findViewById(R.id.btn_6);
        btn7 = (Button)this.findViewById(R.id.btn_7);
        btn8 = (Button)this.findViewById(R.id.btn_8);
        btn9 = (Button)this.findViewById(R.id.btn_9);
        btnEqual = (Button)this.findViewById(R.id.btn_equal);

        btnClear.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnDivide.setOnClickListener(this);
        btnMultiply.setOnClickListener(this);
        btnPlus.setOnClickListener(this);
        btnSubtract.setOnClickListener(this);
        btnPoint.setOnClickListener(this);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnEqual.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (flagClear){
            txtResult.setText("0");
            flagClear = false;
        }

        String txt = txtResult.getText().toString();

        switch (v.getId()){
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                if (txt.equals("0")){
                    txt="";
                }
                txtResult.setText(txt+((Button)v).getText());
                break;
            case R.id.btn_plus:
            case R.id.btn_substract:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                txtResult.setText(txt+" "+((Button)v).getText()+" ");
                break;
            case R.id.btn_del:
                if (txt.equals("0") == false) {
                    txtResult.setText(txt.substring(0, txt.length() - 1).trim());
                }
                break;
            case R.id.btn_clear:
                txtResult.setText("0");
                break;
            case R.id.btn_equal:
                flagClear = true;
                txtResult.setText(getResult(getResult(txt)));
                break;
        }
    }

    /**
     * 获取计算结果字符串
     * @param value
     * @return
     */
    private String getResult(double value){

        if (value % 1.0 == 0){
            return String.valueOf((int)value);
        }else{
            return String.valueOf(value);
        }
    }

    /**
     * 计算结果
     * @param formula
     * @return
     */
    private double getResult(String formula){
        if (formula == null || formula.equals("")){
            return 0;
        }

        if (formula.contains(" ") == false){
            try{
                return Double.parseDouble(formula);

            } catch (NumberFormatException e){
                return 0;
            }
        }

        //计算结果
        double result = 0;
        //运算符前面的字符串
        String s1 = "";
        //运算符
        String op = "";
        //运算符后面的字符串
        String s2 = formula;
        //空格的索引
        int index = s2.indexOf(" ");

        while (index >= 0){
            //运算符前面的字符串
            s1 = formula.substring(0, index);
            //运算符
            op = formula.substring(index+1,index+2);
            //运算符后面的字符串
            s2 = formula.substring(index+3);

            if (s2.equals("") == false){

                double d1 = 0;
                if (s1.equals("") == false){
                    try {
                        d1 = Double.parseDouble(s1);
                    }catch (NumberFormatException e){
                        d1 = 0;
                    }
                }
                double d2 = 0;
                try {
                    d2 = Double.parseDouble(s2);
                } catch (NumberFormatException e){
                    d2 = getResult(s2);
                }

                if (op.equals("+")){
                    return d1+d2;
                } else if(op.equals("-")){
                    return d1-d2;
                } else if (op.equals("*")){
                    return d1*d2;
                }else if (op.equals("/")){
                    if (d2==0){
                        return 0;
                    }else{
                        return d1/d2;
                    }
                }
            }
        }

        return result;
    }
}
